/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.stakeholder;

import entity.StakeholderEntity;
import entity.BranchEntity;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import session.stateless.StakeholderSessionBeanStateless;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpSession;
import util.entityhelper.StakeholderAccountState;


@ManagedBean
@RequestScoped
public class StakeholderManagedBean implements Serializable {

    @EJB
    private StakeholderSessionBeanStateless stakeholderMgt;
    private Long stakeholderID;
    private String userName;
    private List<StakeholderEntity> stakeholders;
    private StakeholderEntity selectedStakeholder;
    private String nextPage;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private StakeholderAccountState stakeholderAccountState;
    private List<BranchEntity> branches;

    /** Creates a new instance of StakeholderManagedBean */
    public StakeholderManagedBean() {
    }

    public void initView(PhaseEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        HttpSession session = (HttpSession) ctx.getExternalContext().getSession(true);

        //Clear previous set messages(delete)
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();

        stakeholderID = (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId");
        if (stakeholderID == null) {

            //Session not save. (redirect Stakeholder to login.
            setNextPage(ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/StakeholderLogin.xhtml")));
            try {
                ctx.getExternalContext().redirect(getNextPage());
            } catch (IOException ioe) {
                ioe.printStackTrace();

            }
        } else {
            if (getStakeholderAccountState() == null) {
                try {
                    //Clear previous set messages(delete)
                    FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
                    
                    setStakeholderAccountState(stakeholderMgt.getStakeholderPortalInfoById(stakeholderID));
                    userName = stakeholderAccountState.getUsername();
                    System.out.println("userId: " + stakeholderID);
                    populateStakeholderDetails(userName);

                    ctx.getExternalContext().getSessionMap().put("stakeholderEntity", selectedStakeholder);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        }

    }

    public void populateStakeholderDetails(String userName) throws AbortProcessingException, UnsupportedEncodingException {
        System.out.println("what is stakeholder username :" + userName);
        setSelectedStakeholder(stakeholderMgt.retrieveStakeholderInfoByUsername(userName));
        populateBranchList();
    }

    public void populateStakeholderList() throws AbortProcessingException {
        stakeholders = stakeholderMgt.getAllStakeholders();
        System.out.println("size of the stakeholder: " + getStakeholders().size());
        if (getStakeholders().isEmpty()) {
            System.out.println("No Available Stakeholder");
        }

    }

    public void populateBranchList() throws AbortProcessingException {
        branches = stakeholderMgt.viewBranchOutlet(stakeholderID);
        System.out.println("number of branches " + getBranches().size());
        if (getBranches().isEmpty()) {
            System.out.println("no available branches");
        }
    }

    /**
     * @return the stakeholderMgt
     */
    public StakeholderSessionBeanStateless getStakeholderSessionBean() {
        return getStakeholderMgt();
    }

    /**
     * @param stakeholderMgt the stakeholderMgt to set
     */
    public void setStakeholderSessionBean(StakeholderSessionBeanStateless stakeholderSessionBean) {
        this.setStakeholderMgt(stakeholderSessionBean);
    }

    /**
     * @return the stakeholderID
     */
    public long getStakeholderID() {
        return stakeholderID;
    }

    /**
     * @param stakeholderID the stakeholderID to set
     */
    public void setStakeholderID(long stakeholderID) {
        this.stakeholderID = stakeholderID;
    }

    /**
     * @return the stakeholderMgt
     */
    public StakeholderSessionBeanStateless getStakeholderMgt() {
        return stakeholderMgt;
    }

    /**
     * @param stakeholderMgt the stakeholderMgt to set
     */
    public void setStakeholderMgt(StakeholderSessionBeanStateless stakeholderMgt) {
        this.stakeholderMgt = stakeholderMgt;
    }

    /**
     * @return the stakeholders
     */
    public List<StakeholderEntity> getStakeholder() {
        stakeholders = stakeholderMgt.getAllStakeholders();
        System.out.println("size of the stakeholder: " + getStakeholders().size());
        if (getStakeholders().isEmpty()) {
            System.out.println("No Available Stakeholder");
        }

        return getStakeholders();
    }

    /**
     * @param stakeholders the stakeholders to set
     */
    public void setStakeholder(List<StakeholderEntity> stakeholder) {
        this.stakeholders = stakeholder;
    }

    /**
     * @return the selectedStakeholder
     */
    public StakeholderEntity getSelectedStakeholder() {
        return selectedStakeholder;
    }

    /**
     * @param selectedStakeholder the selectedStakeholder to set
     */
    public void setSelectedStakeholder(StakeholderEntity selectedStakeholder) {
        this.selectedStakeholder = selectedStakeholder;
    }

    /**
     * @return the stakeholders
     */
    public List<StakeholderEntity> getStakeholders() {
        return stakeholders;
    }

    /**
     * @return the nextPage
     */
    public String getNextPage() {
        return nextPage;
    }

    /**
     * @param nextPage the nextPage to set
     */
    public void setNextPage(String nextPage) {
        this.nextPage = nextPage;
    }

    /**
     * @return the stakeholderAccountState
     */
    public StakeholderAccountState getStakeholderAccountState() {
        return stakeholderAccountState;
    }

    /**
     * @param stakeholderAccountState the stakeholderAccountState to set
     */
    public void setStakeholderAccountState(StakeholderAccountState stakeholderAccountState) {
        this.stakeholderAccountState = stakeholderAccountState;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the branches
     */
    public List<BranchEntity> getBranches() {
        return branches;
    }

    /**
     * @param branches the branches to set
     */
    public void setBranches(List<BranchEntity> branches) {
        this.branches = branches;
    }
}
